<?php
date_default_timezone_set('Europe/Paris');
 
class Agenda
{
  public $year,  $prev_year,  $next_year;
  public $week,  $prev_week,  $next_week, $current_week;
  
  public $offset;
 
  function __construct()
  {
    $this->week = isset($_GET['week']) ? htmlentities($_GET['week']) : Website::week_format(date('W'));
    $this->year = isset($_GET['jaar']) ? htmlentities($_GET['jaar']) : date('Y');

    $this->prev_year  = $this->next_year  = $this->year;
    $this->prev_week  = $this->next_week  = $this->week;
    
    $this->current_week = date('W');
    $this->offset = date('N', mktime(0,0,0,1,1,$this->year)) - 1;
  }

  public static function time_table()
  {
    $time_table = array();

    $query = mysql_query('SELECT tijd FROM lestijden WHERE type="spinning" ORDER BY tijd');

    while ($row = mysql_fetch_assoc($query))
      $time_table[] = $row['tijd'];

    return $time_table;
  }

  /* Duplicate van time_table : Deze maakt lestijden objecten, om ze te kunnen aanpassen */
  public static function les_table()
  {
    $time_table = array();

    $query = mysql_query('SELECT * FROM lestijden WHERE type="spinning" ORDER BY tijd');

    while ($row = mysql_fetch_assoc($query))
      $time_table[] = new Lestijd($row['ID'], $row['tijd'], $row['type']);

    return $time_table;
  }

  public static function choose_year()
  {
    $current = date('Y');
    return array($current, $current + 1);
  }

  public static function choose_week()
  {
    $current_week = date('W');
    $weeks = array();

    for ($i = 0; $i < 15; $i++)
    {
      if ($current_week + $i <= 52)
        $weeks[] = $current_week + $i;
      else
        $weeks[] = $current_week + $i - 52;
    }

    return $weeks;     
  }

  public static function get_month_nr($week, $year)
  {
    $offset  = date('N', mktime(0,0,0,1,1,$year)) - 1;
    
    $off = array(1, 7);
    $return_months = array();

    $feb     = date('L', mktime(0,0,0,1,1,$year)) ? 29 : 28;
    $months  = array(1 => 31, 2 => $feb, 3 => 31, 4 => 30, 5 => 31, 6 => 30, 7 => 31, 8 => 31, 9 => 30, 10 => 31, 11 => 30, 12 => 31);

    foreach ($off as $day_offset)
    {
      $passed  = $week * 7 + $day_offset;
      $counter = $offset;

      foreach ($months as $month => $days)
      {
        $counter += $days;
        
        if ($passed <= $counter)
        {
          $return_months[] = $month;
          break;
        }
      }
    
      $passed = 0;
      $counter = 0;
    }

    if (isset($return_months[1]) && $return_months[0] == $return_months[1])
      return $return_months[0];
    else 
      return $return_months;  
  }
 
  public static function get_month_name($week, $year)
  {
    $month = Agenda::get_month_nr($week, $year);

    if (is_array($month) && isset($month[1]))
      return Agenda::month_nr_to_name($month[0]). " - " . Agenda::month_nr_to_name($month[1]);
    else
      return Agenda::month_nr_to_name($month);
  }

  public static function get_month_name_for_day($day, $week, $year)
  {
    $month = Agenda::get_month_nr($week, $year);

    if (is_array($month))
    {
      if ($day > 23)
        return Agenda::month_nr_to_name($month[0]);
      else
        return Agenda::month_nr_to_name($month[1]);
    }
    else
      return Agenda::month_nr_to_name($month);
  }

  public static function month_nr_to_name($nr)
  {
    /* Ik weet dat je de locale kunt aanpassen, maar die is ook nog eens afhankelijk van Systeem etc */
    $months = array("Januari", "Januari", "Februari", "Maart", "April", "Mei", "Juni", "Juli", "Augustus", "September", "Oktober", "November", "December");
    return $months[$nr%13];
  }

  public static function day_name_to_nr($day_name)
  {
    switch ($day_name)
    {
      case "Maandag"   : return 1;
      case "Dinsdag"   : return 2;
      case "Woensdag"  : return 3;
      case "Donderdag" : return 4;
      case "Vrijdag"   : return 5;
      case "Zaterdag"  : return 6;
      case "Zondag"    : return 7;
    }
  }

  public static function days()
  {
    return array("Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag", "Zaterdag", "Zondag");
  }

  function get_date()
  {
    return "Week ".$this->week." ".Agenda::get_month_name($this->week, $this->year)." ".$this->year;
  }    
      
  function link_prevWeek($additional='')
  {
    if ($this->week == 1)
    {
      $this->prev_week = 52;
      $this->prev_year--;
    }
    else
    {
      $this->prev_week--;
    }

    return "<a href='".BASE_URL."/$additional?week=".$this->prev_week."&jaar=".$this->prev_year."' class='prev' title='Vorige week tonen'>Vorige week</a>";
  }

  function link_nextWeek($additional='')
  {
    if ($this->week == 52)
    {
      $this->next_week = 1;
      $this->next_year++;
    }
    else
    {
      $this->next_week++;
    }

    return "<a href='".BASE_URL."/$additional?week=".$this->next_week."&jaar=".$this->next_year."' class='next' title='Volgende week tonen'>Volgende week</a>";
  }
  
  function printDays()
  { 
    $time_table = Agenda::time_table();
    $max_deelnemers = Les::get_max_deelnemers();
  
    $html = "";

    foreach ($time_table as $time)
    {
      $html .= "<tr>";
      $html .= "\n<td class='time'>$time</td>";
   
      for ($i = 1; $i <= 7; $i++)
      { 
        $nr      = $this->display_day_nr($i);
        $html   .= "\n<td>";

        $les_id  = Les::get_id($nr, $this->week, $this->year, $time);
        $query   = mysql_query('SELECT COUNT(user_ID) FROM '.DB_DEELNEMER.' WHERE les_ID="'.$les_id.'"');
        $row     = mysql_fetch_assoc($query);
        $counter = $row['COUNT(user_ID)'];

        if ($counter > 0)
        {
          if ($_SESSION['user_ID'] == 0)
            $html .= "<a href='".BASE_URL."/view/admin.php?d=".$nr."/".$this->week."/".$this->year."&t=$time' class='les'>Fietsen</a>";
          else
            $html .= "<a href='".BASE_URL."/view/dag.php?d=".$nr."/".$this->week."/".$this->year."&t=$time' class='les'>Fietsen</a>";

          $html .= "\n<span class='nr'>Vrij : ". ($max_deelnemers - $counter)."</span>";
        }

        $html .= "</td>";
      }

      $html .= "\n</tr>";
    }
    
    return $html;
  }
 
  function display_day_nr($i)
  {
    $month = Agenda::get_month_nr($this->week, $this->year);
    if (is_array($month))
      $month = $month[0];

    $total_days_passed    = ($this->week) * 7;
    $days_in_month_passed = date('z', mktime(0,0,0, $month,1,$this->year)) + $this->offset;

    return date('j', mktime(0,0,0, $month, ($total_days_passed + $i) - $days_in_month_passed, $this->year));  
  }

  public static function get_display_nr($weekday, $week, $month, $year)
  {
    $total_days_passed = $week * 7;
    $offset = date('N', mktime(0,0,0,1,1,$year)) - 1;
    $days_in_month_passed = date('z', mktime(0,0,0,$month,1,$year)) + $offset;

    return date('j', mktime(0,0,0,$month,($total_days_passed + $weekday) - $days_in_month_passed, $year));  
  }

  public static function get_next_week_nr($week)
  {
    if ($week <= 52)
      return $week + 1;
    else
      return 1;
  }

  public static function get_next_year_nr($week, $year)
  {
    if ($week <= 52)
      return $year;
    else
      return $year + 1;
  }

  public static function get_next_day_nr($week_day, $week, $year)
  {
    $month = Agenda::get_month_nr($week, $year);

    if (is_array($month))
    {
      $tmp_a = Agenda::get_display_nr($week_day, $week, $month[0], $year);

      if (isset($month[1]))
        $tmp_b = Agenda::get_display_nr($week_day, $week, $month[1], $year);

      if (! isset($tmp_b))
        return $tmp_a;

      if ($tmp_b == 1 || $tmp_b > 22)
        return $tmp_a;
      else
        return $tmp_b;
    }
    else
      return Agenda::get_display_nr($week_day, $week, $month, $year);
  }

  public static function create_week_array($from, $to)
  {
    $length = $to - $from;

    if ($length < 0)
      $length += 52;

    for ($i = 0; $i <= $length; $i++)
    {
      $week_nr = $i + $from;

      if ($week_nr <= 52)
        $weeks[] = $week_nr;
      else
        $weeks[] = $week_nr - 52;
    }

    return $weeks;
  }
}
?>